# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=qt-creator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
__pre=
_base_ver=5.0.3
pkgver=${_base_ver}${_pre}
pkgrel=1
pkgdesc='Cross-platform IDE (mingw-w64)'
url='https://www.qt.io/'
install=qt-creator-${MSYSTEM}.install
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-quick3d"
         "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
         "${MINGW_PACKAGE_PREFIX}-make"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp")
optdepends=("${MINGW_PACKAGE_PREFIX}-gdb: for the debugger"
            "${MINGW_PACKAGE_PREFIX}-cmake: CMake project support"
            "${MINGW_PACKAGE_PREFIX}-ninja: Ninja Generator"
            "${MINGW_PACKAGE_PREFIX}-qbs: QBS project support"
            "${MINGW_PACKAGE_PREFIX}-clang: Clang code model"
            "${MINGW_PACKAGE_PREFIX}-qt5-doc: integrated Qt documentation"
            #'openssh-askpass: for ssh support'
            #'git: for git support'
            #'mercurial: for mercurial support'
            #'bzr: for bazaar support'
            )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-analyzer"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-mlir"
             "${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt5-doc")
options=('docs' 'staticlibs') # 'debug' '!strip')
_pkgfqn="${_realname}-opensource-src-${_base_ver}${__pre}"
source=(#https://download.qt.io/development_releases/qtcreator/${_base_ver%.*}/${_base_ver}${__pre}/${_pkgfqn}.tar.xz
        https://download.qt.io/official_releases/qtcreator/${pkgver%.*}/${pkgver}/${_pkgfqn}.tar.xz
        qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch
        qt-creator-5.0.1-fix-library-archive-path.patch)
noextract=(${_pkgfqn}.tar.xz)
sha256sums=('7fd3b14310bcecee6b134f8cb7f4c0ed2385d7f4cecd48d756c5c1fd650ce2eb'
            '96c14f54577bf6cadf5c12018745666a9e99cd8d6a876c29a28b13599a8cb368'
            '4daf74e55e9b5cae9f05704a98fb832688edf958481cfb2246aecf6b54156e53')

prepare() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}
  tar -xf ${srcdir}/${_pkgfqn}.tar.xz -C "${srcdir}" || true

  cd ${srcdir}/${_pkgfqn}

  patch -p1 -i "${srcdir}"/qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch
  patch -p1 -i "${srcdir}"/qt-creator-5.0.1-fix-library-archive-path.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  if check_option "debug" "y"; then
    _build_type=Debug
  else
    _build_type=Release
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DWITH_DOCS=ON \
    -DBUILD_DEVELOPER_DOCS=ON \
    -DBUILD_QBS=OFF \
    ../${_pkgfqn}

  cmake --build .
  cmake --build . --target docs
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR="${pkgdir}" cmake --install .
  DESTDIR="${pkgdir}" cmake --install . --component Devel
  DESTDIR="${pkgdir}" cmake --install . --component qch_docs
  DESTDIR="${pkgdir}" cmake --install . --component html_docs

  install -Dm644 ${srcdir}/${_pkgfqn}/LICENSE.GPL3-EXCEPT ${pkgdir}${MINGW_PREFIX}/share/licenses/qtcreator/LICENSE.GPL3-EXCEPT
}

# Please leave this block here as I use
# it to quickly make and test changes.
# return 0
# Debugger ProjectExplorer 
# ALLGOOD="yes"
# PLUGINS="Debugger"
# for PLUGIN in $PLUGINS; do
#   PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#   pushd src/build-x86_64-w64-mingw32/src/plugins/${PLUGINLC}
#     PATH=/mingw64/bin:"$PATH" make || ALLGOOD="no"
#   popd
# done
# if [ "${ALLGOOD}" = "yes" ]; then
# (
#   QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   while [ -n "$QTCPID" ]; do
#     cmd.exe /c "taskkill /PID $QTCPID"
#     sleep 1
#     QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   done
#   for PLUGIN in $PLUGINS; do
#     PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#     pushd src/build-x86_64-w64-mingw32/src/plugins/${PLUGINLC}
#       PATH=/mingw64/bin:$PATH make
#       cp ../../../lib/qtcreator/plugins/${PLUGIN}d.dll /mingw64/lib/qtcreator/plugins/
#     popd
#   done
#   PATH=/mingw64/bin:$PATH /mingw64/bin/qtcreator &
#   )
# fi
# 
